package com.imooc.api.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created with IntelliJ IDEA.
 *
 * @auther Levi
 * @Date: 2021/03/21/13:42
 * @Description:
 */
@Configuration
public class RabbitMQDelayConfig {

    public static final String EXCHANGE_DELAY = "exchange_delay";
    public static final String QUEUE_DELAY = "queue_delay";

    /**
     * 创建Exchange
     */
    @Bean(EXCHANGE_DELAY)
    public Exchange exchange() {
        return ExchangeBuilder
                .topicExchange(EXCHANGE_DELAY)//创建交换机，使用topic类型，并且定义名称
                .delayed()//设置延迟消息
                .durable(true)//持久化，重启MQ之后仍然存在
                .build();//build创建
    }

    /**
     * 创建queue
     * @return
     */
    @Bean(QUEUE_DELAY)
    public Queue queue() {
        return new Queue(QUEUE_DELAY);//创建Queue
    }

    /**
     * 把Queue   绑定到   Exchange
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    public Binding DelayBinding(
            @Qualifier(QUEUE_DELAY) Queue queue,
            @Qualifier(EXCHANGE_DELAY) Exchange exchange) {
        return BindingBuilder//定义绑定关系
                .bind(queue)//绑定queue
                .to(exchange)//绑定到exchange
                .with("delay.#")//定义路由规则
                .noargs();//执行绑定
    }


}
